package basicComp;

import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JPanel;

/**
 * 基础面板类
 * @author NHQ
 */

public class BasicMenuPane extends JPanel
{
	private static final long serialVersionUID = 3700398680927901859L;
	private int x,y,w,h,align;
	private int transX,transY;
	private boolean vertical;
	
	private Image backImage;
	
	ArrayList<BasicArtButton> buttons = new ArrayList<BasicArtButton>();
	public BasicMenuPane(int buttonNum,boolean isFirst)
	{
		this.setBackground(Color.WHITE);
		this.setLayout(null);
		this.setVisible(isFirst);
		for(int c = 0 ; c <buttonNum ; c++)
		{
			buttons.add(new BasicArtButton(""));
			this.add(buttons.get(c));
		}
		
	}
	/**
	 * 自定义位置关系
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 * @param align
	 * @param vertical
	 */
	public void init(int x, int y ,int w, int h,int align , boolean vertical)
	{
		System.out.println(x+"-"+y+"-"+w+"-"+h+"-"+align+"-"+vertical);
		this.x = x ;this.y = y ; this.w = w ; this.h = h ;this.align = align ; this.vertical = vertical; 
		
		
		for(int c = 0 ; c < buttons.size() ;c++)
		{
			if(vertical)
			{
				buttons.get(c).setBounds(x, y+align*c, w, h);
			}else
			{
				buttons.get(c).setBounds(x+align*c, y, w, h);
			}
		}
	}
	/**
	 * 居中关系
	 * @param w
	 * @param h
	 * @param align
	 * @param vertical
	 */
	public void init(int w,int h,int align,boolean vertical)
	{
		if(vertical)
		{
			int allHeight = h*buttons.size() + (align-h)*(buttons.size()-1);
			int iniy = (this.getHeight() - allHeight)/2;
			int x = (this.getWidth() - w)/2;
			init(x,iniy,w,h,align,vertical);
		}else
		{
			int allWeigth = w*buttons.size() + (align-w)*(buttons.size()-1);
			int inix = (this.getWidth()-allWeigth)/2;
			int y = (this.getHeight() -h)/2;
			init(inix,y,w,h,align,vertical);
			System.out.println(allWeigth + "--"+inix);
		}
	}
	/**
	 * 以当前按钮位置为准移动x.y像素
	 * @param x
	 * @param y
	 */
	public void transform(int x , int y)
	{
		transX +=x;transY+=y;
		for(int c = 0 ; c < buttons.size() ;c++)
		{
			buttons.get(c).setLocation(buttons.get(c).getX()+x, buttons.get(c).getY()+y);
		}
	}
	
	public void setNames(String[] names)
	{
		for(int c = 0 ; c < buttons.size() ;c++)
		{
			buttons.get(c).setText(names[c]);
			buttons.get(c).setName(names[c]);
		}
	}
	/**
	 * 设置点击按钮显示的面板
	 * @param index
	 * @param c
	 */
	public void setButtonLink(int index,final Component c)
	{
		buttons.get(index).addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
				c.setVisible(true);
			}
		});
	}
	/**
	 * 设置结束游戏按钮(退出)
	 * @param index
	 */
	public void setExitButton(int index)
	{
		buttons.get(index).addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		
	}
	public void paint(Graphics g)
	{
		super.paint(g);
		this.setBounds(0, 0, this.getParent().getWidth(), this.getParent().getHeight());
		
	}
	/**
	 * 设置背景图片
	 * @param m
	 */
	public void setBackImage(Image m)
	{
		this.backImage = m;
		repaint();
	}
	
	/**
	 * 设置改变按钮颜色
	 */
	public void changeColor(Color color1,Color color2,Color color3){
		for(int i=0;i<buttons.size();i++){
			buttons.get(i).setColor(color1, color2, color3);
		}
		
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if(backImage != null)
		{
			g.drawImage(backImage, 0, 0,this.getWidth(),this.getHeight(),null);
		}
	}
	
	public void addButtonListener(int index , ActionListener al)
	{
		buttons.get(index).addActionListener(al);
	}
	
	
}
